
headerbar {
  min-height: $large_size;
  background-color: $headerbar_backdrop_color;
  color: $headerbar_fg_color;
  margin: 0;
  @extend %toolbar;

  @if $rimless == 'false' {
    box-shadow: inset 0 -1px $headerbar_border_color;
  } @else {
    border-radius: $window-radius $window-radius 0 0;
    box-shadow: inset 0 -1px $headerbar_border_color, inset 0 1px $window_outline_color;
  }

  > windowhandle > box {
    padding: $base_padding;

    > box.start,
    > box.end {
      border-spacing: $base_padding;
    }

    &, > widget {
      > box.start:dir(ltr),
      > box.end:dir(rtl) {
        margin-right: $base_padding;
      }

      > box.start:dir(rtl),
      > box.end:dir(ltr) {
        margin-left: $base_padding;
      }
    }
  }

  &:backdrop {
    background-color: $headerbar_bg_color;
    transition: background-color $backdrop_transition;

    > windowhandle {
      // opacity looks weird with GtkSwitch, but filter works fine
      filter: opacity(0.5);

      transition: filter $backdrop_transition;
    }
  }

  &.default-decoration {
    padding: 0;
    margin: 0;
    border: none;
    min-height: $medium_size;

    @if $rimless == 'false' {
      box-shadow: none;
    } @else {
      box-shadow: inset 0 1px $window_outline_color;
    }

    > windowhandle > box {
      padding: $base_padding / 2;
    }

    windowcontrols {
      > button {
        @if $window_button == 'mac' {
          min-width: 16px;
          min-height: 16px;
          margin: 0 $base_padding - 2px;
          padding: 0;
        } @else {
          min-width: 22px;
          min-height: 22px;
          margin: 0 $base_padding - 2px;
          padding: 0;
        }
      }

      > .icon {
        margin: $base_padding;
      }
    }
  }

  .maximized &,
  .fullscreen &,
  .tiled &,
  .tiled-top &,
  .tiled-right &,
  .tiled-bottom &,
  .tiled-left &,
  .solid-csd & {
    border-radius: 0;
    box-shadow: inset 0 -1px $headerbar_border_color;
  }
}

window > {
  .titlebar:not(.flat) {
    box-shadow: inset 0 -1px $headerbar_border_color;
  }

  .titlebar headerbar:not(.flat) {
    box-shadow: inset 0 -1px $headerbar_border_color;
  }

  headerbar.titlebar,
  .titlebar headerbar {
    min-height: $large_size;

    > windowhandle > box {
      padding-bottom: $base_padding;
    }

    &.default-decoration {
      min-height: $medium_size;
      box-shadow: none;

      > windowhandle > box {
        padding: $base_padding / 2;
      }
    }
  }
}

leaflet:first-child > headerbar {
  & + separator {
    background-color: $headerbar_backdrop_color;

    @if $rimless == 'false' {
      box-shadow: inset 0 -1px $headerbar_border_color;
    } @else {
      box-shadow: inset 0 -1px $headerbar_border_color, inset 0 1px $window_outline_color;
    }

    &:backdrop {
      background-color: $headerbar_bg_color;
      transition: background-color $backdrop_transition;
    }
  }
}

%headerbar-flat {
  background: none;
  color: inherit;
  min-height: $large_size;

  @if $rimless == 'false' {
    box-shadow: none;
  } @else {
    box-shadow: inset 0 1px $window_outline_color;
  }

  > windowhandle > box {
    padding-bottom: $base_padding;
  }

  &.default-decoration {
    min-height: $medium_size;

    > windowhandle > box {
      padding: $base_padding / 2;
    }
  }

  windowcontrols > button {
    @if $window_button =='mac' {
      &.minimize, &.maximize, &.close {
        &:backdrop {
          > image {
            background-color: gtkalpha($window_fg_color, 0.3);
          }
  
          &:hover, &:active {
            color: gtkalpha($window_fg_color, 0.45);
          }
        }
      }
    }
  }

  .maximized &,
  .fullscreen &,
  .tiled &,
  .tiled-top &,
  .tiled-right &,
  .tiled-bottom &,
  .tiled-left &,
  .solid-csd & {
    box-shadow: none;
  }
}

%headerbar-inline {
  @extend %headerbar-flat;

  &:backdrop {
    transition: none;

    > windowhandle {
      filter: none;
      transition: none;
    }
  }

  window.devel & > windowhandle {
    background-image: none;
  }
}

%headerbar-shrunk {
  min-height: $large_size - $base_padding;

  > windowhandle > box {
    padding-top: $base_padding / 2;
    padding-bottom: $base_padding / 2;
  }

  &.default-decoration {
    min-height: $medium_size;

    > windowhandle > box {
      padding-top: 0px;
      padding-bottom: 0px;
    }
  }
}

.titlebar:not(headerbar) {
  separator { background-color: $headerbar_border_color; }
}

/*********************
 * GtkWindowControls *
 *********************/

$titlebutton_min: $button-min;
$titlebutton_max: $button-max;
$titlebutton_close: $button-close;

%mac_window_button {
  min-height: 16px;
  min-width: 16px;
  padding: ($medium_size - 16px) / 2 0;
  margin-left: $base_padding / 2 + 1px;
  margin-right: $base_padding / 2 + 1px;
  box-shadow: none;

  &.minimize, &.maximize, &.close {
    color: transparent;

    &, &:hover, &:active, &:disabled {
      box-shadow: none;
      background-color: transparent;
      background-image: none;
    }

    &:hover, &:active {
      color: if($colorscheme != 'dracula', white, rgba(black, 0.5));
    }

    &:backdrop {
      > image { background-color: gtkalpha($headerbar_fg_color, 0.3); }

      &:hover, &:active {
        color: gtkalpha($headerbar_fg_color, 0.45);
      }
    }
  }

  $button-opacity: if($topbar == 'dark', 25%, 12%);

  &.minimize { // Window minimize button
    > image { background-color: $titlebutton_min; }

    &:active > image {
      background-color: gtkmix(black, $titlebutton_min, $button-opacity);
    }
  }

  &.maximize { // Window maximize button
    > image { background-color: $titlebutton_max; }

    &:active > image {
      background-color: gtkmix(black, $titlebutton_max, $button-opacity);
    }
  }

  &.close { // Window close button
    > image { background-color: $titlebutton_close; }

    &:active > image {
      background-color: gtkmix(black, $titlebutton_close, $button-opacity);
    }
  }
}

%normal_window_button {
  min-height: 22px;
  min-width: 22px;
  padding: ($medium-size - 22px) / 2 0;
  margin-left: $base_padding / 2 + 1px;
  margin-right: $base_padding / 2 + 1px;

  &.minimize, &.maximize, &.close {
    color: gtkalpha(currentColor, 0.75);

    &, &:hover, &:active, &:backdrop {
      background: none;
      box-shadow: none;
    }

    > image { background-color: gtkalpha(currentColor, 0.1); }

    &:hover {
      color: currentColor;

      > image { background-color: gtkalpha(currentColor, 0.15); }
    }

    &:active {
      color: currentColor;

      > image { background-color: gtkalpha(currentColor, 0.2); }
    }

    &:backdrop {
      opacity: 0.65;
    }
  }
}

windowcontrols {
  border-spacing: $base_padding;

  &:not(.empty) {
    &.start:dir(ltr),
    &.end:dir(rtl) {
      margin-right: $base_padding;
      margin-left: $base_padding;
    }

    &.start:dir(rtl),
    &.end:dir(ltr) {
      margin-left: $base_padding;
      margin-right: $base_padding;
    }
  }

  > button:not(.raised):not(.suggested-action):not(.destructive-action):not(.opaque),
  > button:not(.raised):not(.suggested-action):not(.destructive-action):not(.opaque).image-button {
    @if $window_button == 'mac' {
      @extend %mac_window_button;
    } @else {
      @extend %normal_window_button;
    }

    > image {
      padding: 0;
      border-radius: 100%;
      transition: $button_transition;
    }
  }

  > .icon {
    margin: $base_padding * 1.5;
  }
}

/******************
 * AdwWindowTitle *
 ******************/

headerbar,
windowtitle {
  .title {
    padding-left: $base_padding * 2;
    padding-right: $base_padding * 2;
    font-weight: bold;
  }

  .subtitle {
    font-size: smaller;
    padding-left: $base_padding * 2;
    padding-right: $base_padding * 2;
    @extend .dim-label;
  }
}

windowtitle {
  margin-top: -$base_padding;
  margin-bottom: -$base_padding;
  min-height: 12px;
}

// Development versions of apps to use a differently styled headerbar

window.devel {
  headerbar > windowhandle {
    background-image: cross-fade(5% -gtk-recolor(url("assets/devel-symbolic.svg")),
                                 image(transparent));
    background-repeat: repeat-x;
  }

  dialog headerbar > windowhandle {
    background-image: unset;
    background-repeat: unset;
  }
}
